//index.js
const util = require('../../util/util.js');
import { RequestManager} from '../../util/requestManager.js'
let requestManager = new RequestManager()

const app = getApp();

Page({
  data: {
    conentMarginTop:0,
    currentCategoryItem: {},
    categoryItemList: Array(),
    goodsItemList: Array(),
    currentPageItem:{},   //当前显示的page

    refreshEnabled:true,      //listView 是否支持下拉刷新
    headerRefreshing:false,   //顶部是否正在刷新
    footerRefreshing:false,   //底部是否正在刷新
    upRefreshEnabled:false,    //listView 是否支持上拉刷新

    topTipsSpaceHeight:60      //顶部提示语高度 60/140
  },
  onLoad: function (options) {

    if(options) {
      //  发现推广者的id
      // console.log('发现推广者信息',options);
      var userid = options.id;
      if (userid == null) {
        //  则是通过分销者分销的二维码进入小程序的
        let tmpInfo = decodeURIComponent(options.scene);
        let valueItems = tmpInfo.split("=")
        if(valueItems.length == 2) {
          let key = valueItems[0]
          if (key === "id") {
            userid = valueItems.pop()
          }
        }
      }

      if(userid != null){
        requestManager.bindParentOpenId(app.globalData.openid,userid,result=>{
        })
      }
    }

    this.setData({
      conentMarginTop: app.globalData.navHeight + 45
    })
    this.private_downloadCategoryData()
  },

  //  顶部分类选择
  categoryDidSelected: function (event) {
    var tmpCategory = event.detail;
    this.setData({
      currentCategoryItem: tmpCategory,
      goodsItemList: [],
      headerRefreshing : true
    })
    this.private_downloadGoodsItemData(tmpCategory, 1);
  },
  //  商品被点击
  didSelectedItem: function (event) {
    var goodsId = event.currentTarget.id;
    wx.navigateTo({
      url: '/pages/detail/goods-detail-page?goodsId='+ goodsId,
    })
  },

  //  获取分类数据
  private_downloadCategoryData: function () {
    var that = this
    requestManager.getCategoryItemList(result=>{
      //  默认选中第一个分类
      result.forEach((tmpItem, index) => {
        tmpItem.selected = index == 0 ? true : false;
      })
      //  默认获取第一个分类下的商品信息
      that.private_downloadGoodsItemData(result[0],1);

      //  设置Data的数据，进行刷新界面
      that.setData({
        categoryItemList: result,
        currentCategoryItem: result[0]
      })
    })
  },
  //  获取分类下商品数据
  private_downloadGoodsItemData:function(categoryItem,pageIndex){
    var that = this
    requestManager.getGoodsItemList(categoryItem,pageIndex, result=>{

      //  如果当前pageInde = 1时
      var tmpPageItem = {}
      if(pageIndex == 1){
        tmpPageItem = result
      }
      else {
        tmpPageItem = that.data.currentPageItem;

        var tmpGoodsItems = tmpPageItem.goodsItems

        tmpPageItem.goodsItems = tmpGoodsItems.concat(result.goodsItems);
        tmpPageItem.current_page = result.current_page;
      }
      let tmpupRefreshEnabled = result.next_page_url === null ? false : true

      //  2020-06-02,排序处理，按最新的产品排序,除了第一个产品
      // let tmpSortItems = tmpPageItem.goodsItems;
      // let firstItem = tmpSortItems.reverse().pop();
      // tmpSortItems.sort(function(a,b){
      //   if (a.created_at == null || b.created_at == null) {
      //     return -1;
      //   }
      //   var aTime = new Date(a.created_at);
      //   var bTime = new Date(b.created_at);

      //   if (aTime > bTime) {
      //     return -1;
      //   }
      //   else {
      //     return 1
      //   }
      // });
      // tmpSortItems.splice(0,0,firstItem)

      that.setData({
        currentPageItem: tmpPageItem,
        upRefreshEnabled: tmpupRefreshEnabled,
        headerRefreshing: false,
        footerRefreshing: false,
        goodsItemList:tmpPageItem.goodsItems
      })
      wx.hideLoading();
    })
  },
  //  下拉刷新
  scroll_refresh:function(event){
    let tmpCategoryItem = this.data.currentCategoryItem;
    this.private_downloadGoodsItemData(tmpCategoryItem,1)
  },
  //  上拉加载更多
  scroll_loadingMore:function(event){
    wx.showLoading({
      title: '正在加载更多中...',
    })

    let tmpCategoryItem = this.data.currentCategoryItem;
    let tmpPageIndex = this.data.currentPageItem.current_page + 1
    this.private_downloadGoodsItemData(tmpCategoryItem, tmpPageIndex)
  },
  //  分享到其他
  onShareAppMessage:function(){
    return app.public_getShareInfo()
  },

  //  产品排序
  private_sort:function(a,b){
    if(a.create_at == null || b.create_at == null) {
      return -1;
    }
    var aTime = new Date(a.create_at);
    var bTime = new Date(b.create_at);
    if(aTime > bTime){
      return -1;
    }
    else{
      return 1
    }
  },
  //  点击事件
  action_open:function(){
    wx.navigateTo({
      url: '/pages/index/web-page',
    })
  },

  //
  bindload:function(event){
    this.setData({
      topTipsSpaceHeight: 140
    })
  },
  binderror:function(event){
    this.setData({
      topTipsSpaceHeight: 60
    })
  }
})
